package com.dsg.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.security.SystemAuthorizingUser;
import com.dsg.common.util.DateUtils;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.account.DsgAccountCardMapper;
import com.dsg.dao.account.DsgAccountTradeMapper;
import com.dsg.entity.account.DsgAccountCard;
import com.dsg.entity.account.DsgAccountTrade;
import com.dsg.entity.apply.DsgApplyCard;
import com.dsg.entity.apply.DsgApplyDiscount;
import com.dsg.entity.bank.DsgBankDetail;
import com.dsg.entity.coupon.DsgCouponReturn;
import com.dsg.entity.oil.DsgOilDataDetail;
import com.dsg.entity.oil.DsgOilProduct;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.account.IDsgAccountCardService;
import com.dsg.service.account.IDsgAccountTradeService;
import com.dsg.service.apply.IDsgApplyBillService;
import com.dsg.service.apply.IDsgApplyCardService;
import com.dsg.service.apply.IDsgApplyDiscountService;
import com.dsg.service.coupon.IDsgCouponReturnService;
import com.dsg.service.oil.IDsgOilDataDetailService;
import com.dsg.service.oil.IDsgOilProductService;
import com.dsg.support.LeveldbAccess;
import com.dsg.util.Constant;
import com.dsg.util.RRException;
import com.dsg.util.SnowflakeKeyWorker;
import com.dsg.util.VersionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 交易帳目 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@Service
public class DsgAccountTradeServiceImpl extends DsgServiceImpl<DsgAccountTradeMapper, DsgAccountTrade> implements IDsgAccountTradeService {

    @Autowired
    private IDsgOilDataDetailService dsgOilDataDetailService;
    @Autowired
    private IDsgCouponReturnService dsgCouponReturnService;
    @Autowired
    private IDsgApplyDiscountService dsgApplyDiscountService;
    @Autowired
    private IDsgApplyCardService dsgApplyCardService;
    @Autowired
    private IDsgOilProductService dsgOilProductService;
    @Autowired
    private IDsgAccountCardService dsgAccountCardService;
    @Autowired
    private IDsgAccountTradeService dsgAccountTradeService;
    @Autowired
    private DsgAccountCardMapper dsgAccountCardMapper;
    @Autowired
    private DsgAccountTradeMapper dsgAccountTradeMapper;
    @Autowired
    private LeveldbAccess leveldbAccess;
    @Autowired
    private IDsgApplyBillService billService;

    @Override
    public void writeTradingAccounts(DsgBankDetail dsgBankDetail) {
        SystemAuthorizingUser sysUser = SingletonLoginUtils.getSystemUser();
        List<DsgOilDataDetail> dsgOilDataDetailList = dsgOilDataDetailService.selectDsgBankOilDataDetail(dsgBankDetail);
        List<String> keyList = new ArrayList<>();
        //交易賬目
        List<DsgAccountTrade> dsgAccountTradeList = new ArrayList<>();
        //回填交易賬目總金額
        Map<String, DsgAccountCard> dsgAccountCardMap = new HashMap<>();
        try {
            if (CollectionUtil.isNotEmpty(dsgOilDataDetailList)) {
                String oid = UUIDGenerator.getUUID();
                Date date = new Date();
                //缓存回赠油券资料 cusNo
                QueryWrapper<DsgCouponReturn> dcRQueryWrapper = new QueryWrapper();
                dcRQueryWrapper.eq("ENT_CODE", sysUser.getOrgCode());
                dcRQueryWrapper.gt(true, "EFF_DATE", DateUtils.formatDate(date));
                dcRQueryWrapper.gt(true, "EXP_DATE", DateUtils.formatDateTime(date));
                dcRQueryWrapper.orderByAsc("EXP_DATE");
                List<DsgCouponReturn> dsgCouponReturnList = dsgCouponReturnService.list(dcRQueryWrapper);
                //分組回赠油券资料，因可能會存在多條並重算油總金額時需扣減
                Map<String, List<DsgCouponReturn>> dsgCouponReturnMap = dsgCouponReturnList.stream().collect(Collectors.groupingBy(I -> I.getCusNo()));
                for (String keyStr : dsgCouponReturnMap.keySet()) {
                    String key = leveldbAccess.genCacheKey(oid, DsgCouponReturn.class.getSimpleName(), keyStr);
                    leveldbAccess.put(key, dsgCouponReturnMap.get(keyStr));
                    keyList.add(key);
                }
                dsgCouponReturnList.clear();
                //缓存折扣資料
                QueryWrapper<DsgApplyDiscount> dadQueryWrapper = new QueryWrapper();
                dadQueryWrapper.select("CUS_NO,max(DIS_LIMIT) as DIS_LIMIT,max(CHEEP_AMOUNT) as CHEEP_AMOUNT,max(COSTLY_AMOUNT) as COSTLY_AMOUNT");
                dadQueryWrapper.eq("ENT_CODE", sysUser.getOrgCode());
                dadQueryWrapper.eq("DIS_STATUS", DsgApplyDiscount.AppStatus.approved.getCode());
                dadQueryWrapper.and(i -> i.in("DISCOUNT_TYPE", 2, 3).gt(true, "EFF_DATE", date).le(true, "EXP_DATE", date));
                dadQueryWrapper.or(i -> i.notIn("DISCOUNT_TYPE", 2, 3));
                dadQueryWrapper.groupBy("CUS_NO");
                List<DsgApplyDiscount> dsgApplyDiscountList = dsgApplyDiscountService.list(dadQueryWrapper);
                dsgApplyDiscountList.forEach(I -> {
                    String key = leveldbAccess.genCacheKey(oid, DsgApplyDiscount.class.getSimpleName(), I.getCusNo());
                    leveldbAccess.put(key, I);
                    keyList.add(key);
                });
                dsgApplyDiscountList.clear();
                dadQueryWrapper = new QueryWrapper();
                dadQueryWrapper.select("CARD_NO,max(DIS_LIMIT) as DIS_LIMIT,max(CHEEP_AMOUNT) as CHEEP_AMOUNT,max(COSTLY_AMOUNT) as COSTLY_AMOUNT");
                dadQueryWrapper.eq("ENT_CODE", sysUser.getOrgCode());
                dadQueryWrapper.eq("DIS_STATUS", DsgApplyDiscount.AppStatus.approved.getCode());
                dadQueryWrapper.and(i -> i.in("DISCOUNT_TYPE", 2, 3).gt(true, "EFF_DATE", date).le(true, "EXP_DATE", date));
                dadQueryWrapper.or(i -> i.notIn("DISCOUNT_TYPE", 2, 3));
                dadQueryWrapper.groupBy("CARD_NO");
                dsgApplyDiscountList = dsgApplyDiscountService.list(dadQueryWrapper);
                dsgApplyDiscountList.forEach(I -> {
                    String key = leveldbAccess.genCacheKey(oid, DsgApplyDiscount.class.getSimpleName(), I.getCardNo());
                    leveldbAccess.put(key, I);
                    keyList.add(key);
                });
                dsgApplyDiscountList.clear();
                //緩存行政費
                QueryWrapper<DsgApplyCard> dacQueryWrapper = new QueryWrapper();
                dacQueryWrapper.eq("ENT_CODE", sysUser.getOrgCode());
                dacQueryWrapper.eq("CARD_STATUS", DsgApplyCard.AppStatus.inForce.getCode());
                List<DsgApplyCard> dsgApplyCardList = dsgApplyCardService.list(dacQueryWrapper);
                dsgApplyCardList.forEach(I -> {
                    String key = leveldbAccess.genCacheKey(oid, DsgApplyCard.class.getSimpleName(), I.getCardNo());
                    leveldbAccess.put(key, I);
                    keyList.add(key);
                });
                dsgApplyCardList.clear();
                //缓存油產品
                QueryWrapper<DsgOilProduct> dopQueryWrapper = new QueryWrapper();
                dopQueryWrapper.eq("ENT_CODE", sysUser.getOrgCode());
                List<DsgOilProduct> dsgOilProductList = dsgOilProductService.list(dopQueryWrapper);
                dsgOilProductList.forEach(I -> {
                    String key = leveldbAccess.genCacheKey(oid, DsgOilProduct.class.getSimpleName(), I.getProductNo());
                    leveldbAccess.put(key, I);
                    keyList.add(key);
                });
                dsgOilProductList.clear();
                //回填有效的現金抵扣金額
                List<DsgCouponReturn> updateDcrList = new ArrayList<>();
                //取油咭總賬并缓存
                List<DsgAccountCard> dsgAccountCardList = dsgAccountCardService.getOrCreateDsgAccountCard(dsgOilDataDetailList);
                dsgAccountCardList.forEach(I -> {
                    String key = leveldbAccess.genCacheKey(oid, DsgAccountCard.class.getSimpleName(), I.getCardNo());
                    leveldbAccess.put(key, I);
                    keyList.add(key);
                });
                dsgAccountCardList.clear();
                Map<String, String> cardNoMap = new HashMap<>();
                //銀行往來明細資料
                BigDecimal arrearsValue = BigDecimal.ZERO;
                //油咭最後交易賬目OID
                String pOid = null;
                for (DsgOilDataDetail dsgOilDataDetail : dsgOilDataDetailList) {
                    //取油咭結欠總金額
                    if (!cardNoMap.containsKey(dsgOilDataDetail.getCardNo())) {
                        arrearsValue = BigDecimal.ZERO;
                        String key = leveldbAccess.genCacheKey(oid, DsgAccountCard.class.getSimpleName(), dsgOilDataDetail.getCardNo());
                        DsgAccountCard dsgAccountCard = leveldbAccess.get(key);
                        if (dsgAccountCard != null) {
                            arrearsValue = dsgAccountCard.getArrearsValue();
                            pOid = dsgAccountCard.getTradeId();
                        }
                        cardNoMap.put(dsgOilDataDetail.getCardNo(), dsgOilDataDetail.getCardNo());
                    }
                    /**
                     *
                     * 直接使用入油牌價
                     */
                    //入油牌價
                    BigDecimal oilReValue = dsgOilDataDetail.getOilValue();
                    //入油升數
                    BigDecimal oilLiters = dsgOilDataDetail.getOilLiters();
                    //油的單價
                    BigDecimal oilPrice = dsgOilDataDetail.getOilPrice();
                    String key = leveldbAccess.genCacheKey(oid, DsgCouponReturn.class.getSimpleName(), dsgOilDataDetail.getCusNo());
                    List<DsgCouponReturn> dsgCouponReturns = leveldbAccess.get(key);
                    if (CollectionUtil.isNotEmpty(dsgCouponReturns)) {
                        /**
                         * 處理多條回贈油券資料
                         */
                        //回填回贈油券資料緩存
//                        List<DsgCouponReturn> dcrList = new ArrayList<>();
                        //有效的現金抵扣金額-交易賬目（總金額）
                        BigDecimal balAmountSum = BigDecimal.ZERO;
                        for (DsgCouponReturn dsgCouponReturn : dsgCouponReturns) {
                            //有效的現金抵扣金額
                            BigDecimal balAmount = dsgCouponReturn.getBalAmount();
                            if (balAmount.compareTo(oilReValue) < 0) {
                                //入油牌價-有效的現金抵扣金額
                                oilReValue = oilReValue.subtract(balAmount);
                                balAmountSum = balAmountSum.add(balAmount);
                                dsgCouponReturn.setBalAmount(BigDecimal.ZERO);
                                leveldbAccess.delete(key);
                            }
//                            else {
//                                dsgCouponReturn.setBalAmount(balAmount.subtract(oilReValue));
//                                balAmountSum = balAmountSum.add(oilReValue);
//                                dcrList.add(dsgCouponReturn);
//                                oilReValue = BigDecimal.ZERO;
//                            }
                            updateDcrList.add(dsgCouponReturn);
                        }
//                        if (CollectionUtil.isNotEmpty(dcrList)) {
//                            leveldbAccess.put(key, dcrList);
//                        } else {
//                            leveldbAccess.delete(key);
//                        }
                        //有效的現金抵扣金額-交易賬目
                        arrearsValue = setDsgAccountTrade(arrearsValue, "IN", dsgOilDataDetail, balAmountSum, dsgAccountTradeList, DsgAccountTrade.AppStatus.cashDeduValue.getCode(), pOid);
                        pOid = dsgOilDataDetail.getTradeId();
                    }
                    //油公司產品 平油/貴油
                    Integer oilGrade = 0;
                    key = leveldbAccess.genCacheKey(oid, DsgOilProduct.class.getSimpleName(), dsgOilDataDetail.getProductNo());
                    DsgOilProduct dsgOilProduct = leveldbAccess.get(key);
                    if (dsgOilProduct != null) {
                        oilGrade = dsgOilProduct.getOilGrade();
                    }
                    /**
                     * 折扣
                     */
                    //折扣金額(平油/貴油)
                    Integer amount = null;
                    //客戶折扣金額(平油/貴油)
                    Integer cusNoAmount = null;
                    //咭號折扣金額(平油/貴油)
                    Integer cardNoAmount = null;
                    //客戶
                    key = leveldbAccess.genCacheKey(oid, DsgApplyDiscount.class.getSimpleName(), dsgOilDataDetail.getCusNo());
                    DsgApplyDiscount dsgApplyDiscount = leveldbAccess.get(key);
                    if (dsgApplyDiscount != null) {
                        switch (oilGrade) {
                            case 1:
                                cusNoAmount = dsgApplyDiscount.getCheepAmount();
                                break;
                            case 2:
                                cusNoAmount = dsgApplyDiscount.getCostlyAmount();
                                break;
                        }
                    }
                    //咭號
                    key = leveldbAccess.genCacheKey(oid, DsgApplyDiscount.class.getSimpleName(), dsgOilDataDetail.getCardNo());
                    dsgApplyDiscount = leveldbAccess.get(key);
                    if (dsgApplyDiscount != null) {
                        switch (oilGrade) {
                            case 1:
                                cardNoAmount = dsgApplyDiscount.getCheepAmount();
                                break;
                            case 2:
                                cardNoAmount = dsgApplyDiscount.getCostlyAmount();
                                break;
                        }
                    }
                    if (cusNoAmount != null && cardNoAmount != null) {
                        //取客戶或咭號最優惠金額
                        if (cusNoAmount.intValue() > cardNoAmount.intValue()) {
                            amount = cusNoAmount;
                        } else {
                            amount = cardNoAmount;
                        }
                    } else {
                        if (cusNoAmount != null) {
                            amount = cusNoAmount;
                        } else {
                            if (cardNoAmount != null) {
                                amount = cardNoAmount;
                            }
                        }
                    }
                    // 折扣金额=折扣优惠*升数 ： （3*67.54）
                    if (amount != null) {
                        amount = amount / 100;
                        BigDecimal discount = new BigDecimal(amount).multiply(oilLiters);
                        //交易賬目－折扣
                        arrearsValue = setDsgAccountTrade(arrearsValue, "IN", dsgOilDataDetail, discount, dsgAccountTradeList, DsgAccountTrade.AppStatus.discountValue.getCode(), pOid);
                        pOid = dsgOilDataDetail.getTradeId();
                    }
                    //行政費
                    key = leveldbAccess.genCacheKey(oid, DsgApplyCard.class.getSimpleName(), dsgOilDataDetail.getCardNo());
                    DsgApplyCard dsgApplyCard = leveldbAccess.get(key);
                    if (dsgApplyCard != null) {
                        //行政费金额 =（牌价单价-折扣优惠）*升数*行政费百分比
                        BigDecimal adminCost = oilPrice.subtract(amount != null ? new BigDecimal(amount) : BigDecimal.ZERO).multiply(oilLiters).multiply(dsgApplyCard.getAdminCost());
                        //交易賬目－行政費
                        arrearsValue = setDsgAccountTrade(arrearsValue, "OUT", dsgOilDataDetail, adminCost, dsgAccountTradeList, DsgAccountTrade.AppStatus.adminValue.getCode(), pOid);
                        pOid = dsgOilDataDetail.getTradeId();
                    }
                    //交易賬目－交易金額
                    arrearsValue = setDsgAccountTrade(arrearsValue, "OUT", dsgOilDataDetail, oilReValue, dsgAccountTradeList, DsgAccountTrade.AppStatus.oilIn.getCode(), pOid);
                    pOid = dsgOilDataDetail.getTradeId();
                    key = leveldbAccess.genCacheKey(oid, DsgAccountCard.class.getSimpleName(), dsgOilDataDetail.getCardNo());
                    DsgAccountCard dsgAccountCard = leveldbAccess.get(key);
                    if (dsgAccountCard != null) {
                        dsgAccountCard.setArrearsValue(arrearsValue);
                        dsgAccountCard.setTradeId(dsgOilDataDetail.getTradeId());
                        leveldbAccess.put(key, dsgAccountCard);
                        dsgAccountCardMap.put(dsgOilDataDetail.getCardNo(), dsgAccountCard);
                    }
                    dsgOilDataDetail.setOilReValue(oilReValue);
                    dsgOilDataDetail.setIsAccount(DsgOilDataDetail.AppStatus.alreadyCredit.getCode());
                }
                //保存交易賬目
                dsgAccountTradeService.saveBatch(dsgAccountTradeList);
                //回填回贈油券資料
                dsgCouponReturnService.updateBatchById(updateDcrList);
                //回填交易賬目結欠總金額
                if (CollectionUtil.isNotEmpty(dsgAccountCardMap)) {
                    List<DsgAccountCard> dsgAccountCards = new ArrayList<>(dsgAccountCardMap.values());
                    for (DsgAccountCard dsgAccountCard : dsgAccountCards) {
                        if (dsgAccountCardMapper.updateById(dsgAccountCard) <= 0) {
                            throw new VersionException();
                        }
                    }
                }
                //回填入油數據明細
                dsgOilDataDetailService.updateBatchById(dsgOilDataDetailList);
            }
        } finally {
            if (CollectionUtil.isNotEmpty(keyList)) {
                keyList.forEach(I -> {
                    leveldbAccess.delete(I);
                });
            }
        }
    }

    /**
     * 写交易账目
     *
     * @param arrearsValue        油咭總賬-结欠金額
     * @param inOut               收入/支出（IN/OUT）
     * @param dsgOilDataDetail    上油數據明細
     * @param amount              金额
     * @param dsgAccountTradeList 交易賬目集合
     * @param tranItem            交易專案
     * @param pOid                最後交易賬目OID
     */
    private BigDecimal setDsgAccountTrade(BigDecimal arrearsValue, String inOut, DsgOilDataDetail dsgOilDataDetail,
                                          BigDecimal amount, List<DsgAccountTrade> dsgAccountTradeList,
                                          Integer tranItem, String pOid) {
        //金額為0時不登記交易賬目
        if (BigDecimal.ZERO.compareTo(amount) == 0) {
            return arrearsValue;
        }
        DsgAccountTrade dsgAccountTrade = new DsgAccountTrade();
        dsgAccountTrade.setOid(UUIDGenerator.getUUID());
        dsgAccountTrade.setOilCom(dsgOilDataDetail.getOilCom());
        dsgAccountTrade.setCusNo(dsgOilDataDetail.getCusNo());
        dsgAccountTrade.setCardNo(dsgOilDataDetail.getCardNo());
        //過數方式
        dsgAccountTrade.setCountMode(getPayMode(dsgOilDataDetail.getPayMode()));
        dsgAccountTrade.setInOut(inOut);
        //交易專案
        dsgAccountTrade.setTranItem(tranItem);
        //交易編碼
        dsgAccountTrade.setTranCode(SnowflakeKeyWorker.nextStrId());
        //合併日期
        String transDate = DateUtils.formatDate(dsgOilDataDetail.getTransDate(), "yyyy-MM-dd") + " " + dsgOilDataDetail.getTransTime();
        dsgAccountTrade.setTranDate(DateUtils.parseDate(transDate));
        dsgAccountTrade.setAccountName(dsgOilDataDetail.getAccountName());
        dsgAccountTrade.setAccountNo(dsgOilDataDetail.getAccountNo());
        dsgAccountTrade.setInDate(dsgOilDataDetail.getCreateTime());
        dsgAccountTrade.setOilDataId(dsgOilDataDetail.getOid());
        dsgAccountTrade.setBankCode(dsgOilDataDetail.getPayAccountNo());
        dsgAccountTrade.setBankName(dsgOilDataDetail.getPayAccountName());
        //承前結欠
        dsgAccountTrade.setPreDept(arrearsValue);
        dsgAccountTrade.setTraAmount(amount);
        //總結欠
        switch (inOut) {
            case "OUT":
                arrearsValue = arrearsValue.add(amount);
                break;
            case "IN":
                arrearsValue = arrearsValue.subtract(amount);
                break;
        }
        dsgAccountTrade.setAftDept(arrearsValue);
        dsgAccountTradeList.add(dsgAccountTrade);
        dsgOilDataDetail.setTradeId(dsgAccountTrade.getOid());
        return arrearsValue;
    }

    /**
     * 取付款方式
     *
     * @param payMode
     * @return
     */
    private String getPayMode(String payMode) {
        if (StringUtil.isNotEmpty(payMode)) {
            switch (payMode) {
                case "1":
                    payMode = Constant.PAY_TYPE_CDC;
                    break;
                case "2":
                case "3":
                case "4":
                case "5":
                    payMode = Constant.PAY_TYPE_ATP;
                    break;
            }
            return payMode;
        }
        return null;
    }

    @Override
    public List<DsgAccountTrade> selectDsgAccountTrade(DsgAccountTrade dsgAccountTrade) {
        return dsgAccountTradeMapper.selectDsgAccountTrade(dsgAccountTrade);
    }

    @Override
    public List<DsgAccountTrade> selectDsgAccountTradeListToRecordId(String oid) {
        return dsgAccountTradeMapper.selectDsgAccountTradeListToRecordId(oid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDsgAccountTrade(DsgAccountTrade dsgAccountTrade) {
        DsgAccountCard dsgAccountCard = dsgAccountCardService.getAddDsgAccountCard(dsgAccountTrade.getCardNo());
        if (dsgAccountCard != null) {
            Date date = new Date();
            dsgAccountTrade.setOid(UUIDGenerator.getUUID());
            dsgAccountTrade.setOilCom(dsgAccountCard.getOilCom());
            dsgAccountTrade.setCusNo(dsgAccountCard.getCusNo());
            dsgAccountTrade.setCardNo(dsgAccountCard.getCardNo());
            //交易編碼
            dsgAccountTrade.setTranCode(SnowflakeKeyWorker.nextStrId());
            dsgAccountTrade.setTranDate(date);
            dsgAccountTrade.setAccountName(dsgAccountCard.getAccountName());
            dsgAccountTrade.setAccountNo(dsgAccountCard.getAccountNo());
            dsgAccountTrade.setBankCode(dsgAccountCard.getPayAccountNo());
            dsgAccountTrade.setBankName(dsgAccountCard.getPayAccountName());
            dsgAccountTrade.setInDate(date);
            dsgAccountTrade.setDataStatus(DsgAccountTrade.DATA_STATUS_2);
            //承前結欠
            dsgAccountTrade.setPreDept(BigDecimal.ZERO);
            //總結欠
            dsgAccountTrade.setAftDept(BigDecimal.ZERO);
            //提交審批
            try {
                String appNo = billService.genBillAndGetAppNo(dsgAccountTrade.getCusNo());
                dsgAccountTrade.setAppNo(appNo);
                dsgAccountTradeMapper.insert(dsgAccountTrade);
            } catch (Exception e) {
                log.error("提交審批異常:", e);
                throw new RRException(e.getMessage());
            }
        } else {
            throw new RRException("咭號不存在！");
        }
    }

    @Override
    public IPage<DsgAccountTrade> selectListAdj(Page page, DsgAccountTrade dsgAccountTrade) {
        return page.setRecords(dsgAccountTradeMapper.selectListAdj(page, dsgAccountTrade));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void statement(String cardClassify) {
        String entCode = SingletonLoginUtils.getSystemEntCode();
        DsgAccountTrade dsgAccountTrade = new DsgAccountTrade();
        dsgAccountTrade.setEntCode(entCode);
        if (StringUtil.isNotEmpty(cardClassify)) {
            dsgAccountTrade.setCardClassify(Integer.parseInt(cardClassify));
        }
        List<DsgAccountTrade> tradeList = dsgAccountTradeMapper.selectStatement(dsgAccountTrade);
        if (CollectionUtil.isNotEmpty(tradeList)) {
            Date date = new Date();
            tradeList.forEach(I -> {
                I.setSettlMonth(I.getSettlementMonth());
                I.setStatementDate(I.getSettlMonth());
                I.setCreateDate(date);
            });
            updateBatchById(tradeList);
        }
    }

    @Override
    public IPage<DsgAccountTrade> selectStatementList(Page page, DsgAccountTrade dsgAccountTrade) {
        return page.setRecords(dsgAccountTradeMapper.selectStatementList(page, dsgAccountTrade));
    }

    @Override
    public IPage<DsgAccountTrade> selectStatement(Page page, DsgAccountTrade dsgAccountTrade) {
        return page.setRecords(dsgAccountTradeMapper.selectStatement(page, dsgAccountTrade));
    }
}
